package com.crawler.search.doc.entity;

import com.alibaba.fastjson.JSONObject;
import com.crawler.search.constant.CommonColumn;
import com.crawler.search.constant.ESIndex;
import com.crawler.search.constant.ESType;
import com.crawler.search.constant.QuerierMapper;
import com.crawler.search.index.entity.QueryParam;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchType;

import java.util.ArrayList;


/**
 *
 *新闻检索实体
 * @Class Name ESIndexSaver
 * @author zhangyonghui
 * @Create In 下午3:50:26
 */
public class DocParam extends QueryParam {

	public DocParam(String search) {
		this.searchIndex= ESIndex.DOC;
		this.searchType = ESType.DEFAULT;
		this.search=search;
		buildFieldBoostMap();//设置检索字段
		buildFieldMap();//设置精确检索字段
		buildSegMap();//设置分词检索字段
		buildHighlighter();//设置高亮显示
		buildPubdateRange();//设置范围
		buildShouldMap();
		buildSort();
		buildDistinct();//去重
		buildFetchSource();//返回字段设置
	}

	/**
	 * 查询字段
	 */
	public void buildFieldBoostMap(){
	    this.fieldBoostMap.put(DocField.TITLE, 10F);
	    this.fieldBoostMap.put(DocField.CONTENT, 1F);
	}


	/**
	 * 语言字段,精确匹配,term查询
	 */
	public void buildFieldMap(){
		this.filedMap.put( DocField.LANG, DocField.LANG);
		this.filedMap.put( DocField.CLASSIFYPARENTS, DocField.CLASSIFYPARENTS);
		this.filedMap.put( DocField.MEDIACOUNTRY, DocField.MEDIACOUNTRY);
		this.filedMap.put( DocField.USERID, DocField.USERID);
		this.filedMap.put( DocField.ID, DocField.ID);
		this.filedMap.put( DocField.DOCUMENTID, DocField.COL11);
		this.filedMap.put( DocField.MEDIANAME, DocField.MEDIANAME);
		this.filedMap.put( DocField.MEDIAID, DocField.MEDIAID);

	}

	/**
	 * 分词查询
	 */
	public void buildSegMap(){
		this.segMap.put( DocField.CONTENT, DocField.CONTENT);
		this.segMap.put( DocField.AUTHOR, DocField.AUTHOR);
		this.segMap.put( DocField.ATTACHNAMES, DocField.ATTACHNAMES);
		this.segMap.put( DocField.ORGANIZATION, DocField.ORGANIZATION);
		this.segMap.put( DocField.TITLE, DocField.TITLE);
	}

	/**
	 * should查询字段
	 */
	public void buildShouldMap(){

	}

	/**
	 * 高亮字段
	 */
	public void buildHighlighter(){
		ArrayList<String> list=new ArrayList<String>();
		list.add(DocField.TITLE);
		if(StringUtils.isNotEmpty(getSearch())){
			JSONObject json = JSONObject.parseObject(search);
			if(json.containsKey(QuerierMapper.HIGHLIGHT)) {
				boolean booleanValue = json.getBooleanValue(QuerierMapper.HIGHLIGHT);
				if(!booleanValue) {
					list.clear();
				}
			}
		}
		String[] strings = new String[list.size()];
		this.highlight= list.toArray(strings);
	}

	/**
	 * 排序
	 */
	public void buildSort(){
		if(StringUtils.isNotEmpty(getSearch())){
			JSONObject json = JSONObject.parseObject(search);
			String desc= QuerierMapper.DESC;
			if(json.containsKey(QuerierMapper.ORDER)){
				desc=json.getString(QuerierMapper.ORDER);
			}
			if(json.containsKey(QuerierMapper.SORT_BY)){
				String sortBy = json.getString(QuerierMapper.SORT_BY);
				if(StringUtils.isNotEmpty(sortBy)) {
					this.sortMap.put(sortBy, desc);
				}
			}
		}
	}

	/**
	 * 范围
	 */
	public void buildPubdateRange(){
		buildFieldRange(DocField.PUBDATE);
	}


	public void buildFieldRange(String field){
		if(StringUtils.isNotEmpty(getSearch())){
			JSONObject json = JSONObject.parseObject(search);
			JSONObject rangeTime=new JSONObject();
			if(json.containsKey(QuerierMapper.BEGIN_TIME)){
				String beginTime=json.getString(QuerierMapper.BEGIN_TIME);
				if(StringUtils.isNotEmpty(beginTime)) {
					rangeTime.put(QuerierMapper.BEGIN_TIME, beginTime);
				}
			}
			if(json.containsKey(QuerierMapper.END_TIME)){
				String endTime=json.getString(QuerierMapper.END_TIME);
				if(StringUtils.isNotEmpty(endTime)) {
					rangeTime.put(QuerierMapper.END_TIME, endTime);
				}
			}
			if(json.containsKey(DocField.PUBDATE)) {
				rangeTime.put(DocField.PUBDATE, json.getString(DocField.PUBDATE));
				rangeTime.put(QuerierMapper.FORMAT, CommonColumn.FORMAT_YEAR);
			}
			String key = QuerierMapper.BEGIN_TIME+"|"+QuerierMapper.END_TIME;
			if(json.containsKey(key)) {
				rangeTime.put(key, json.getString(key));
			}
			if(!rangeTime.isEmpty()){
				this.rangeMap.clear();
				this.rangeMap.put(field, rangeTime);
			}
		}
	}



	/**
	 * 查询去重
	 */
	public void buildDistinct(){
		if(StringUtils.isNotEmpty(getSearch())){
			JSONObject json = JSONObject.parseObject(search);
			if(json.containsKey(QuerierMapper.DISTINCT)){
				String distinct=json.getString(QuerierMapper.DISTINCT);
				if(StringUtils.isNotEmpty(distinct)) {
					this.distinct=distinct.split(",");
				}
			}
		}
	}


	/**
	 * 返回字段设置
	 */
	public void buildFetchSource(){
		this.excludeMap.put(DocField.CONTENT, DocField.CONTENT);//排除返回字段
	}

}
